package top.techqi.app.mcs51.usbdlk.tile

import android.hardware.usb.UsbDevice
import android.view.LayoutInflater
import android.view.ViewGroup
import top.techqi.app.mcs51.R
import top.techqi.app.mcs51.databinding.ItemUsbdlkListUsbdBinding
import top.techqi.core.util.ResUtil
import top.techqi.view.recyclerview.tile.BindHolder
import top.techqi.view.recyclerview.tile.TilePortion
import top.techqi.view.recyclerview.tile.TileRecord

class UsbdlkListUsbdPortion : TilePortion<
        UsbdlkListUsbdPortion.Holder,
        UsbdlkListUsbdPortion.Record>() {
    override fun onMakeHolder(inflater: LayoutInflater, container: ViewGroup): Holder {
        return Holder(inflater, container)
    }

    override fun onBindRecord(holder: Holder, record: Record) {
        holder.binding.txtUsbdlkNameValue.text = record.name
        holder.binding.txtUsbdlkMfrsnameIdent.text = ResUtil.getString(
            R.string.item_usbdlk_list_ident_format, record.vendId
        )
        holder.binding.txtUsbdlkMfrsnameValue.text = record.mfrsName
        holder.binding.txtUsbdlkProdnameIdent.text = ResUtil.getString(
            R.string.item_usbdlk_list_ident_format, record.prodId
        )
        holder.binding.txtUsbdlkProdnameValue.text = record.prodName
    }

    class Holder(inflater: LayoutInflater, container: ViewGroup) :
        BindHolder<Record, ItemUsbdlkListUsbdBinding>(
            inflater, container, ItemUsbdlkListUsbdBinding::inflate
        )

    data class Record(
        val name: String,
        val vendId: Int,
        val prodId: Int,
        val mfrsName: String?,
        val prodName: String?,
    ) : TileRecord {
        override val index = name
        var device: UsbDevice? = null

        constructor(
            name: String, vendId: Int, prodId: Int, mfrsName: String?,
            prodName: String?, device: UsbDevice
        ) : this(name, vendId, prodId, mfrsName, prodName) {
            this.device = device
        }
    }
}
